<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        // 格式化日期 时分秒
        // 系统默认的时间格式：Fri Sep 17 2021 17:15:44 GMT+0800 (中国标准时间)
        //                   2021-09-17 17:20:00
        var date = new Date();
        console.log(date.getHours());
        console.log(date.getMinutes());
        console.log(date.getSeconds());

        function getTime() {
            // 时分秒
            var date = new Date();
            var h = date.getHours();
            var m = date.getMinutes();
            var s = date.getSeconds();
            // 补零
            h = h < 10 ? '0' + h : h;
            m = m < 10 ? '0' + m : m;
            s = s < 10 ? '0' + s : s;
            // 拼接
            return h + ':' + m + ':' + s;
        }
        var formatTime = getTime();
        console.log(formatTime);
    </script>
</body>

</html>